home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
HYPERCAR
/
RIDDLE
/
RIDDLE.C
< prev
Wrap
C/C++ Source or Header
|
1990-07-16
|
1KB
|
71 lines
#include <stdio.h>
#include <unix.h>
#include <console.h>
#define random rand
#define srandom srand
#define QUEST_FILE "questions"
#define ANS_FILE "answers"
main(argc,argv)
int argc;
char **argv;
{
argc = ccommand(&argv);
switch(argc) {
case 1: return(ask(QUEST_FILE, 0));
break;
case 2: if (!strcmp(argv[1], "-a")) {
fprintf(stderr, "No answer number!\n");
return(-1);
} else return(ask(QUEST_FILE, atoi(argv[1])));
break;
default: if (!strcmp(argv[1], "-a")) {
return(ask(ANS_FILE, atoi(argv[2])));
break;
} else return(ask(QUEST_FILE, atoi(argv[1])));
}
}
ask(file, q)
char *file;
int q;
{
int count = 0, numquest, num;
int blob;
FILE *InFile;
if (!(InFile = fopen(file, "r"))) {
fprintf(stderr, "Can't open %s\n", file);
exit(-1);
}
fscanf(InFile, "%d", &numquest); /* no. of questions */
if (q == 0) {
srandom(getpid());
num = random() % numquest + 1;
} else
num = q;
if ((num < 1) || (num > numquest)) {
fprintf(stderr, "Invalid question number\n");
exit(-1);
}
while (count < num)
if ((blob = getc(InFile)) == '~') count++ ;
else if (blob == EOF) {
fprintf(stderr, "File not in valid format\n");
exit(-1);
}
while (((blob = getc(InFile)) != '~') && (blob != EOF))
printf("%c", blob);
fclose(InFile);
return(num);
}